package api

import (
	"getaway/basic/config"
	__ "getaway/basic/proto/orderproto"
	"getaway/handler/request"
	"getaway/handler/response"
	"github.com/gin-gonic/gin"
)

// 派单配置添加与修改
func DispatchConfigCreateOrUpdate(c *gin.Context) {
	var req request.DispatchConfigCreateOrUpdate
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	createOrUpdate, err := config.OrderClient.DispatchConfigCreateOrUpdate(c, &__.DispatchConfigCreateOrUpdateReq{
		UserId:              c.GetInt64("userId"),
		CityId:              req.CityId,
		Strategy:            req.Strategy,
		MaxDistanceKM:       float32(req.MaxDistanceKM),
		ScoreWeightDistance: float32(req.ScoreWeightDistance),
		ScoreWeightRating:   float32(req.ScoreWeightRating),
		ScoreWeightAccept:   float32(req.ScoreWeightAccept),
		DayStartHour:        req.DayStartHour,
		NightStartHour:      req.NightStartHour,
		TimeWindowMinutes:   req.TimeWindowMinutes,
		GrayGroups:          req.GrayGroups,
		Version:             req.Version,
	})
	if err != nil {
		response.SrvError(c, "派单配置失败", err.Error())
		return
	}
	response.Success(c, "派单配置添加成功", createOrUpdate)
}

// 获取派单配置
func DispatchConfigGet(c *gin.Context) {
	var req request.DispatchConfigGet
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	createOrUpdate, err := config.OrderClient.DispatchConfigGet(c, &__.DispatchConfigGetReq{
		UserId: c.GetInt64("userId"),
		CityId: req.CityId,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", createOrUpdate)
}

// 删除派单配置
func DispatchConfigDelete(c *gin.Context) {
	var req request.DispatchConfigDelete
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	_, err := config.OrderClient.DispatchConfigDelete(c, &__.DispatchConfigDeleteReq{
		UserId: c.GetInt64("userId"),
		CityId: req.CityId,
	})
	if err != nil {
		response.SrvError(c, "删除失败", err.Error())
		return
	}
	response.Success(c, "删除成功", nil)
}

// 发布派单灰度配置
func DispatchGrayPublish(c *gin.Context) {
	var req request.DispatchGrayPublish
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	publish, err := config.OrderClient.DispatchGrayPublish(c, &__.DispatchGrayPublishReq{
		CityId:     req.CityId,
		Version:    req.Version,
		GrayGroups: req.GrayGroups,
	})
	if err != nil {
		response.SrvError(c, "发布失败", err.Error())
		return
	}
	response.Success(c, "发布成功", publish.Id)
}

// 回滚派单灰度配置
func DispatchGrayRollback(c *gin.Context) {
	var req request.DispatchGrayRollback
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	publish, err := config.OrderClient.DispatchGrayRollback(c, &__.DispatchGrayRollbackReq{
		CityId:  req.CityId,
		Version: req.Version,
	})
	if err != nil {
		response.SrvError(c, "回滚失败", err.Error())
		return
	}
	response.Success(c, "回滚成功", publish.Id)
}

// 查询派单审计记录
func DispatchAuditQuery(c *gin.Context) {
	var req request.DispatchAuditQuery
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "参数错误", err.Error())
		return
	}
	audit, err := config.OrderClient.DispatchAuditQuery(c, &__.DispatchAuditQueryReq{
		CityId:  req.CityId,
		Version: req.Version,
	})
	if err != nil {
		response.SrvError(c, "查询失败", err.Error())
		return
	}
	response.Success(c, "查询成功", audit)
}
